﻿using _08_Prism框架.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace _08_Prism框架.ViewModels;

public class MainViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;
    private readonly IDialogService _dialogService;
    private readonly IEventAggregator _aggregator;

    /// <summary>
    /// 区域导航日志
    /// </summary>
    private IRegionNavigationJournal? _navigationJournal;
    public MainViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator aggregator)
    {
        Title = "Prism框架Demo";

        _regionManager = regionManager;
        _dialogService = dialogService;
        _aggregator = aggregator;

        OpenCommand = new DelegateCommand<string>(Open);
        OpenDialogCommand = new DelegateCommand(OpenDialog);
        BackCommand = new DelegateCommand(Back);

        // 订阅来自MessageEevent事件的消息
        _aggregator.GetEvent<MessageEvent>().Subscribe(SubMessageEvent);
    }

    private void SubMessageEvent(string arg)
    {
        // 收到消息对话框显示
        MessageBox.Show($"接收到消息:{arg}");
        // 收到消息后取消订阅
        _aggregator.GetEvent<MessageEvent>().Unsubscribe(SubMessageEvent);
    }

    public DelegateCommand<string> OpenCommand { get; private set; }
    public DelegateCommand OpenDialogCommand { get; private set; }
    public DelegateCommand BackCommand { get; private set; }

    private string? _title;

    public string? Title
    {
        get { return _title; }
        set { _title = value; RaisePropertyChanged(); }
    }

    private string? _message;

    public string? Message
    {
        get { return _message; }
        set { _message = value; RaisePropertyChanged(); }
    }


    private void Open(string viewName)
    {
        // 创建导航参数
        NavigationParameters keys = new NavigationParameters();
        keys.Add("Title", "导航发送的参数");
        // 导航到指定视图，并传递参数
        _regionManager
            .Regions["ContentRegion"]
            .RequestNavigate(viewName, callback =>
            {
                if (callback.Result ?? false)
                {   // 保存导航日志
                    _navigationJournal = callback.Context.NavigationService.Journal;
                }
            }, keys);
        Title = viewName;
    }

    private void OpenDialog()
    {
        DialogParameters keys = new DialogParameters();
        keys.Add("Title", "弹窗测试");
        _dialogService.ShowDialog("ViewC", keys, callback =>
        {
            if (callback.Result == ButtonResult.OK)
            {
                Message = "按下了确认，并返回了一个参数："
                + callback.Parameters.GetValue<string>("Value");
            }
            else
            {
                Message = "按下了取消";
            }
        });
    }

    private void Back()
    {
        if (_navigationJournal?.CanGoBack ?? false)
        {
            _navigationJournal?.GoBack();
        }
    }

}
